#pragma once
#include"const.h"

class RedisConPool {
public:
	RedisConPool(const char* host,int port,size_t poolsize, const char* pwd);
	~RedisConPool();
	redisContext* GetContext();

	void ReturnContext(redisContext* context);
	void Close();

private:
	std::atomic<bool> b_stop_;
	const char* host_;
	int port_;
	size_t poolsize_;
	std::mutex mutex_;
	std::condition_variable cond_;
	std::queue<redisContext*> context_;
};

class RedisMgr:public Singleton<RedisMgr>
{
	friend Singleton<RedisMgr>;
public:
	~RedisMgr();
	bool Connect(const std::string& host, int port);
	bool Get(const std::string& key,std::string& value);
	bool Set(const std::string& key, const std::string& value);
	bool Auth(const std::string& password);
	bool LPush(const std::string& key, const std::string& value);
	bool LPop(const std::string& key, const std::string& value);
	bool RPush(const std::string& key, const std::string& value);
	bool RPop(const std::string& key, std::string& value);
	bool HSet(const std::string& key, const std::string& hkey, const std::string& value);
	bool HSet(const char* key, const char* hkey, const char* hvalue, size_t hvaluelen);
	std::string HGet(const std::string& key, const std::string& hkey);
	bool Del(const std::string& key);
	bool HDel(const std::string& key, const std::string& field);
	bool ExistsKey(const std::string& key);
	void Close();
private:
	RedisMgr();
	
	redisContext* _connect;
	redisReply* _reply;

	std::unique_ptr<RedisConPool> con_pool_;
};

